package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.entity.data.entity.PcSupplementStatus;
import com.alks.entity.data.enums.SlipTypeEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.biocontrol.deptwork.SupTraceDtos;
import com.alks.function.data.request.biocontrol.deptwork.SupTraceRequest;
import com.alks.function.data.vo.biocontrol.deptwork.SupTraceExcelVo;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcSupplementStatusMapper;
import com.alks.function.service.biocontrol.deptwork.ISuppTraceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class SuppTraceServiceImpl extends ServiceImpl<BC_DW_PcSupplementStatusMapper, PcSupplementStatus> implements ISuppTraceService {
    private final BC_DW_PcSupplementStatusMapper statusMapper;

    @Override
    @AutoPageAop
    public ResponseInfo getSupTraceQuery(SupTraceRequest request) {
        /*数据转换*/
        if (request.getBlFlag() != null) {
            request.setBlFlag(SlipTypeEnum.SlipTypeFind(request.getBlFlag()));
        }
        /*获取数据*/
        List<SupTraceDtos> dtos = statusMapper.getSupTraceQuery(request, UserIdThread.get().getCompanyId());
        for (SupTraceDtos dto : dtos) {
            dto.setBlFlag(SlipTypeEnum.SlipNameFind(dto.getBlFlag()));
        }
        /*封装数据*/
        Map<String, Object> map = new HashMap<>();
        map.put("dtos", dtos);
        if (request.getPageSize() != -1) {
            Page<SupTraceDtos> page = (Page<SupTraceDtos>) dtos;
            map.put("total", page.getTotal());
        }
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo supTraceUpdate(PcSupplementStatus request) {
        /*处理入参*/
        try {
            for (int i = 1; i <= 7; i++) {
                Method typeFlagMethod = PcSupplementStatus.class.getMethod("type" + i + "NeedFlag");
                Method finishDateMethod = PcSupplementStatus.class.getMethod("finishDate" + i, LocalDateTime.class);

                String typeFlag = (String) typeFlagMethod.invoke(request);
                if ("Y".equals(typeFlag) && finishDateMethod.invoke(request) == null) {
                    finishDateMethod.invoke(request, LocalDateTime.now());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*更新数据*/
        lambdaUpdate().eq(PcSupplementStatus::getSlipNo, request.getSlipNo())
                .eq(PcSupplementStatus::getLotNo, request.getLotNo())
                .eq(PcSupplementStatus::getCompanyId, UserIdThread.get().getCompanyId())
                .set(PcSupplementStatus::getType1NeedFlag, request.getType1NeedFlag())
                .set(PcSupplementStatus::getType2NeedFlag, request.getType2NeedFlag())
                .set(PcSupplementStatus::getType3NeedFlag, request.getType3NeedFlag())
                .set(PcSupplementStatus::getType4NeedFlag, request.getType4NeedFlag())
                .set(PcSupplementStatus::getType5NeedFlag, request.getType5NeedFlag())
                .set(PcSupplementStatus::getType6NeedFlag, request.getType6NeedFlag())
                .set(PcSupplementStatus::getType7NeedFlag, request.getType7NeedFlag())
                .set(PcSupplementStatus::getTypeNeedFlag, request.getTypeNeedFlag())
                .set(PcSupplementStatus::getFinishDate1, request.getFinishDate1())
                .set(PcSupplementStatus::getFinishDate2, request.getFinishDate2())
                .set(PcSupplementStatus::getFinishDate3, request.getFinishDate3())
                .set(PcSupplementStatus::getFinishDate4, request.getFinishDate4())
                .set(PcSupplementStatus::getFinishDate5, request.getFinishDate5())
                .set(PcSupplementStatus::getFinishDate6, request.getFinishDate6())
                .set(PcSupplementStatus::getFinishDate7, request.getFinishDate7())
                .set(PcSupplementStatus::getFinishDate, request.getFinishDate()).update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo supTraceDelete(String slipNo, String lotNo) {
        /*删除数据*/
        LambdaQueryWrapper<PcSupplementStatus> wrapper = new LambdaQueryWrapper<PcSupplementStatus>()
                .eq(PcSupplementStatus::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(PcSupplementStatus::getSlipNo, slipNo)
                .eq(PcSupplementStatus::getLotNo, lotNo);
        statusMapper.delete(wrapper);
        return ResponseInfo.ok();
    }

    @Override
    public List<SupTraceExcelVo> getSupTraceExprod(SupTraceRequest request) {
        /*数据转换*/
        if (request.getBlFlag() != null) {
            request.setBlFlag(SlipTypeEnum.SlipTypeFind(request.getBlFlag()));
        }
        /*获取数据*/
        List<SupTraceExcelVo> dtos = statusMapper.getSupTraceExprod(request, UserIdThread.get().getCompanyId());
        for (SupTraceExcelVo dto : dtos) {
            dto.setBlFlag(SlipTypeEnum.SlipNameFind(dto.getBlFlag()));
        }
        return dtos;
    }
}
